var express = require('express'),
    path = require('path'),
    favicon = require('serve-favicon'),
    logger = require('morgan'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    app = express(),
    //普通用户
    index = require('./routes/index'),//首页
    register = require('./routes/register'),//注册路由
    login = require('./routes/login'),//登陆路由
    update = require('./routes/update'),//修改密码
    error = require('./routes/error'),//失败
    newUser = require('./routes/newUser'),//新注册用户
    readerGarden = require('./routes/readerGarden'),//读者广场
    readbook = require('./routes/readbook'),//阅读书籍
    kinds = require('./routes/kinds'),//图书种类
    //admin
    adminCenter = require('./adminRoutes/adminCenter'),
    admin = require('./adminRoutes/admin'),//管理员登陆路由
    del = require('./adminRoutes/delete'),//删除账号
    talkDel = require('./adminRoutes/talkDel'),//删除评论
    addBook = require('./adminRoutes/addBook'),//增加图书
    delBook = require('./adminRoutes/delBook');//删除图书

app.use('/index',index);//首页
app.use('/register', register);//注册
app.use('/addBook', addBook);//增加图书
app.use('/delete',del);//删除
app.use('/talkDel',talkDel);//删除评论
app.use('/delBook',delBook);//删除图书
app.use('/update',update);//修改
app.use('/error',error);//失败
app.use('/login',login);//登陆
app.use('/admin',admin);//登陆
app.use('/newUser',newUser);//新用户页面
app.use('/readerGarden',readerGarden);//新用户页面
app.use('/readbook',readbook);//阅读书籍
app.use('/kinds',kinds);//图书种类
app.use('/adminCenter',adminCenter);//admin中心

// view engine setup
app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'ejs');
//模板后缀是.ejs，现改为.html
app.engine('.html',require('ejs').__express);
app.set('view engine','html');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);//404跳转到error页面
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});
app.listen(8000,function(){
    console.log("Server has Started at port 8000");
});

module.exports = app;
